package fr.uvsq.isty.bdh.hchess.engine;

import fr.uvsq.isty.bdh.hchess.gui.game.BoardDisplay;
import fr.uvsq.isty.bdh.hchess.gui.style.Style;
import ictk.boardgame.chess.Bishop;
import ictk.boardgame.chess.Knight;
import ictk.boardgame.chess.Queen;
import ictk.boardgame.chess.Rook;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;

/**
 * A button that allow the player to choose in wich piece a pawn will be promoted.
 * @author Rémi Dubois <RemiEric.Dubois@gmail.com>
 */
public class PiecePromotionButton extends JButton implements Observer {
    
    private Style style; /** Board style singleton */
    
    private final int pieceId;
    private boolean isBlackPromotion;
    private boolean actionListenerSet;
    
    /**
     * @param piece the kind of piece in wich this button will promote.
     */
    public PiecePromotionButton (int piece) {
        this.setPreferredSize(new Dimension(
                        BoardDisplay.DEFAULT_CASE_WIDTH,
                        BoardDisplay.DEFAULT_CASE_HEIGHT));
        
        try {
            this.style = Style.getInstance();
        } catch (IOException ex) {
        }
        
        this.pieceId = piece;
        
        actionListenerSet = false;
        this.repaint();
    }
    
    @Override
    public void paintComponent(Graphics g) {
        Image image = null;
        Image bg = style.getCaseImage(!isBlackPromotion);
        
        g.drawImage(bg, 0, 0, this.getWidth(), this.getHeight(), this);
        
        if(pieceId == Queen.INDEX) {
            image = style.getPieceImage("queen", isBlackPromotion);
        }
        else if(pieceId == Rook.INDEX) {
            image = style.getPieceImage("rook", isBlackPromotion);
        }
        else if(pieceId == Bishop.INDEX) {
            image = style.getPieceImage("bishop", isBlackPromotion);
        }
        else if(pieceId == Knight.INDEX) {
            image = style.getPieceImage("knight", isBlackPromotion);
        }
        
        g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
    }

    /**
     * Actualise the button, if it's the first time he update he will connect 
     * himself to the GameData so by pressing it the promotion will be triggered.
     * @param o
     * @param arg 
     */
    public void update(Observable o, Object arg) {
        if(o instanceof GameData) {
            final GameData gameData = (GameData) o;
            
            if(!actionListenerSet) {
                this.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        gameData.setPromotion(pieceId);
                    }
                });
                actionListenerSet = true;
            }
            
            if(gameData.isPromoting()) {
                isBlackPromotion = gameData.isBlackTurnToPlay();
                this.repaint();
            }
        }
    }
}
